import type { Group } from '@/store/modules/dataCount'

/**
 * @desc 一天的毫秒数
 */
const ONE_DAY_TIME = 24 * 60 * 60 * 1000

/**
 * @desc 生成整年空数据
 */
export function initData(year: number) {
  const firstDay = new Date(year + '').getDay()
  const totalDay = new Date(year, 2, 0).getDate() + firstDay + 365 - 28
  const Data: Group[] = [
    {
      baseDate: new Date(year + '').getTime() - firstDay * ONE_DAY_TIME,
      count: []
    }
  ]
  for (let [i, j] = [0, 0]; i < totalDay; i++, j++) {
    Data[Data.length - 1]?.count.push(0)
    if (j === 6 && i !== totalDay - 1) {
      j = -1
      Data.push({
        baseDate: new Date(Data[Data.length - 1].baseDate as number).getTime() + 7 * ONE_DAY_TIME,
        count: []
      })
    }
  }
  return Data
}

/**
 * @param today 当前时刻
 * @returns 当前是本年的第几周(0,1,2...)
 */
export function getYearWeek(today: Date): number {
  const firstDay = new Date(today.getFullYear() + '')
  const firstDayOfWeek = firstDay.getDay()
  const dayOfYear = (today.getTime() - firstDay.getTime()) / ONE_DAY_TIME
  return ~~((~~dayOfYear + firstDayOfWeek - 1) / 7)
}
